<template>
  <div>
    <!-- 导航栏 -->
    <van-nav-bar title="设置昵称" left-text="返回" right-text="完成" @click-left="$emit('close')" @click-right="updataUser"> </van-nav-bar>
    <!-- 文本框 -->
    <div>
      <van-field v-model="localName" rows="2" autosize type="textarea" maxlength="7" placeholder="请输入昵称" show-word-limit />
    </div>
  </div>
</template>

<script>
import { updataUsersAPI } from '@/api/User.js'
export default {
  name: 'updataName',
  props: {
    value: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      localName: this.value
    }
  },
  methods: {
    async updataUser() {
      this.$toast.loading({
        message: '保存中...',
        forbidClick: true, //禁止背景点击
        duration: 0
      })
      try {
        if (!this.localName) {
          this.$toast.fail('昵称不能为空！')
          return
        }
        await updataUsersAPI({
          name: this.localName
        })
        // 更新视图
        this.$emit('input', this.localName)
        //关闭弹出层
        this.$emit('close')
        //提示成功
        this.$toast.success('更新成功')
      } catch (err) {
        this.$toast.fail('修改昵称失败，请稍后重试！')
        console.log(err)
      }
    }
  }
}
</script>

<style lang="less" scoped></style>
